<?php
require_once('../qcubed.inc.php');
require_once('panels/SchoolMarketingEditPanel.class.php');
require_once('panels/SchoolStagePanel.class.php');
require_once('panels/SchoolStageEditPanel.class.php');
require_once('panels/SchoolUsedTitlesListPanel.class.php');
require_once('panels/SchoolUsedTitlesEditPanel.class.php');
require_once('panels/CompetitorEditPanel.class.php');
require_once('panels/CompetitorListPanel.class.php');

//Add Auth

//Panel
//require_once('panels/ComSchoolListPanel.class.php');

class MarketingForm extends QForm {

    //Local instance of mct
    public $mctOrg;
    private $mctSeason;
    //Local istance of data obj
    public $objOrg;

    //panel Space Holders
    protected $URL;

    protected $pnlMarektingEdit;
    protected $pnlSchoolUsedTitles;
    protected $pnlStage;
    protected $pnlCompetitorList;

    protected $authSys;
    //Controls
    protected $txtAOrg;
    protected $txtAOracleLink;
    protected $lstSeason;
    protected $btnViewList;
    //Tab
    public $tabMain;

    //*****************Events
    //{
    public function txtAOrg_Change($strParameter) {
        return $this->authSys->AutoList_MySchoolList($strParameter, "Name");
    }

    public function txtAOracleLink_Change($strParameter) {
        return $this->authSys->AutoList_MySchoolList($strParameter, "OracleLink");
    }

    public function btnViewList_Click() {

        if ( strlen($this->txtAOrg->Text) > 0 ) {
            $this->objOrg    = Org::LoadByName($this->txtAOrg->Text);
            $OrgFound = true;
            $_SESSION['Org'] = $this->objOrg;
            $this->URL =  $_SERVER["SCRIPT_NAME"]  . "/" . QS_ENCODE($this->objOrg->Id);
        }
        elseif ( strlen($this->txtAOracleLink->Text) > 0 ) {
            $this->objOrg    = Org::LoadByOracleLink( $this->txtAOracleLink->Text );
            $OrgFound = true;
            $_SESSION['Org'] = $this->objOrg;
            $this->URL =  $_SERVER["SCRIPT_NAME"]  . "/" . QS_ENCODE($this->objOrg->Id);
        }else {
            $OrgFound = "";
            $this->URL =  $_SERVER["SCRIPT_NAME"]  ;
        }
        $this->txtAOrg->Text        = "";
        $this->txtAOracleLink->Text = "";
        if ($OrgFound == true) {
            if ( $this->authSys->Check_Org_Perm(  $_SESSION['Org']->Id, false) == false ) {
                QApplication::DisplayAlert("Cant Access this resources");
                unset($_SESSION['Org']);
                unset($this->objOrg);
                $this->CloseAllPane();
                $this->ClosePanel("pnlMarektingEdit");
            }
            else {
                QApplication::Redirect($this->URL);

            }
        }
    }

    protected function Create_Panels() {
        
        $this->pnlMarektingEdit->RemoveChildControls(true);
        $this->pnlMarektingEdit = new SchoolMarketingEditPanel($this->pnlMarektingEdit, 'ClosePanel');
        $this->pnlMarektingEdit->Visible = true;

        $this->pnlStage->RemoveChildControls(true);
        $this->pnlStage = new SchoolStagePanel($this->pnlStage, 'ClosePanel');
        $this->pnlStage->Visible = true;


        $this->pnlSchoolUsedTitles->RemoveChildControls(true);
        $this->pnlSchoolUsedTitles = new SchoolUsedTitlesListPanel($this->pnlSchoolUsedTitles, 'ClosePanel');
        $this->pnlSchoolUsedTitles->Visible = true;

        $this->pnlCompetitorList->RemoveChildControls(true);
        $this->pnlCompetitorList = new CompetitorListPanel($this->pnlCompetitorList, 'ClosePanel');
        $this->pnlCompetitorList->Visible = true;



        

    }
    //}
    //End of Events
    protected function Form_Run() {
        $this->authSys = new Perm();
        $this->authSys->IsAuthenticated(true);
    }

    protected function Form_Create() {
        $this->mctOrg = OrgMetaControl::CreateFromPathInfo($this);
        $this->objOrg = $this->mctOrg->__get("Org");

        $this->txtAOracleLink = $this->mctOrg->txtAOracleLink_Create();
        $this->txtAOrg        = $this->mctOrg->txtAOrg_Create();

        $this->mctSeason =  SeasonMetaControl::Create($this);
        $this->lstSeason = $this->mctSeason->lstSeason_Create();

        $this->btnViewList = new QButton($this);
        $this->btnViewList->Text = 'View School';
        $this->btnViewList->AddAction(new QClickEvent(), new QAjaxAction('btnViewList_Click'));

        //Create Tabs and Panels
        $this->tabMain = new JTab("MainTab");

        $this->pnlMarektingEdit = new QePanel($this);
        $this->pnlMarektingEdit->AutoRenderChildren = true;
        //tab List All
        $this->tabMain->AddTab(QApplication::Translate("Marketing"),$this->pnlMarektingEdit );


        $this->pnlStage = new QePanel($this);
        $this->pnlStage->AutoRenderChildren = true;
        $this->tabMain->AddTab(QApplication::Translate("Stages"), $this->pnlStage);

        /*
        $this->pnlComEdit = new QePanel($this);
        $this->pnlComEdit->AutoRenderChildren = true;
        //tab Edit
        $this->tabMain->AddTab(QApplication::Translate("Edit School Communications"), null, QApplication::Translate("School Communications"));
        */


        //tab

        $this->pnlSchoolUsedTitles = new QePanel($this);
        $this->pnlSchoolUsedTitles->AutoRenderChildren = true;
        $this->tabMain->AddTab(QApplication::Translate("Used Titles") , $this->pnlSchoolUsedTitles);

        $this->pnlCompetitorList = new QePanel($this);
        $this->pnlCompetitorList->AutoRenderChildren = true;
        $this->tabMain->AddTab(QApplication::Translate("Competitor") , $this->pnlCompetitorList);

        //tab
        //$this->tabMain->AddTab(QApplication::Translate("Samples") , null, QApplication::Translate("School Communications"));

        if ( $this->mctOrg->__get("EditMode") ) {
            $_SESSION['Org'] = $this->objOrg;
            $this->Create_Panels();
        }

    }
    public function CloseAllPane($RefreshPanel=null) {
        $this->AutoClosePane('pnlComEdit');
        $this->AutoClosePane('pnlComInfo');

        $this->AutoClosePane('pnlComNoteEdit');
        $this->AutoClosePane('pnlComNoteList');
        $this->AutoClosePane('pnlComNoteInfo');

        $this->AutoClosePane('pnlCompetitorList');

        //Refresh main panel
        if ($RefreshPanel)
            $this->pnlMarektingEdit->Refresh();

        $this->tabMain->ChangeTabById(0);
    }

    private function AutoClosePane($objParent) {
        //Close the Edit Pane
        $this->$objParent->RemoveChildControls(true);
        $this->$objParent->Visible = false;
    }

    public function ClosePanel($ClosePanel, $RefreshPanel=null) {
        $this->$ClosePanel->RemoveChildControls(true);
        $this->$ClosePanel->Visible = false;

        if ($RefreshPanel)
            $this->$RefreshPanel->Refresh();
    }
    public function SetEditPaneStage(){
        $this->pnlStage->RemoveChildControls(true);
        $this->pnlStage = new SchoolStageEditPanel($this->pnlStage, 'ClosePanel');
        $this->pnlStage->Visible = true;
    }


    public function SetEditPane($objPanel , $objParent) {
        $this->$objParent->RemoveChildControls(true);
        if ($objPanel) {
            $objPanel->SetParentControl($this->$objParent);
            $this->$objParent->Visible = true;
        } else {
            $this->$objParent->Visible = false;
        }
        //$this->tabMain->ChangeTabById(1);
    }
    protected function Form_Validate() {
        // By default, we report that Custom Validations passed
        $blnToReturn = true;

        $blnFocused = false;
        /*                foreach ($this->GetErrorControls() as $objControl) {

                        // Set Focus to the top-most invalid control
                        if (!$blnFocused) {
                                $objControl->Focus();
                                $blnFocused = true;
                                $this->tabMain->ActiveTab =  $objControl->ParentControl->__get("CurrentSection") -1;
                        }
                        $objControl->Blink();
                }*/


        return $blnToReturn;
    }
    public function strTruncate($string, $length) {
        $output="";
        settype($string, 'string');
        settype($length, 'integer');
        for($a = 0; $a < $length AND $a < strlen($string); $a++) {
            $output .= $string[$a];
        }
        return($output);
    }
}

MarketingForm::Run('MarketingForm','tpl/marketing.tpl.php');

?>
